let connection = require("../dbutil/dbutil");
let myModule = require("./my-module");

function getInfo(uid, token, callback) {
    let sql = "select username from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                data: []
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let tsql = "SELECT * FROM shopping_cart_view WHERE uid = '" + uid + "'";
                connection.query(tsql, function (err, result) {
                    if (err) {
                        console.log(err);
                    } else {
                        ret.code = 1001
                        ret.data = result
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

function addTrolley(uid, token, pid, callback) {
    let sql = "select username from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                msg: "加入失败"
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let tsql = "INSERT INTO `trolley` (`uid`, `pid`) VALUES (?, ?)";
                let tsqlparams=[uid, pid]
                connection.query(tsql, tsqlparams, function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        ret.code = 1001   
                        ret.msg = "加入成功"
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}


function getCount(uid, token, callback) {
    let sql = "select username from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                count: 0
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let tsql = "SELECT COUNT(pid) AS total_count FROM trolley WHERE uid = '" + uid + "'";
                connection.query(tsql, function (err, result) {
                    if (err) {
                        console.log(err);
                    } else {
                        ret.code = 1001   
                        ret.count = result[0].total_count
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

function deleteProduct(uid, token, tid, callback) {
    let sql = "select username from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                msg: "删除失败"
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let tsql = "DELETE FROM trolley WHERE tid = '" + tid + "'";
                connection.query(tsql, function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        ret.code = 1001
                        ret.msg = "删除成功"
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

exports.getInfo = getInfo
exports.addTrolley = addTrolley
exports.getCount = getCount
exports.deleteProduct = deleteProduct